package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springboot.common.PageParam;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Poet;
import com.example.springboot.entity.RelationPoet;
import com.example.springboot.entity.RelationType;
import com.example.springboot.service.RelationPoetService;
import com.example.springboot.service.RelationTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 关系api
 */
@RestController
@RequestMapping("/relation")
@RequiredArgsConstructor
public class RelationController {
    private final RelationTypeService relationTypeService;
    private final RelationPoetService relationPoetService;

    @PostMapping("api/save/type")
    public Result saveRelationType(@RequestBody RelationType relationType){
        relationType.insertOrUpdate();
        return Result.success(relationType);
    }

    @PostMapping("api/del/type/{id}")
    public Result delRelationType(@PathVariable Integer id){
        relationTypeService.removeById(id);
        return Result.success();
    }

    @GetMapping("list/type")
    public Result delRelationType(RelationType relationType){
        List<RelationType> relationTypeList = relationTypeService.listRelationType(relationType);
        return Result.success(relationTypeList);
    }

    @GetMapping("count/relation/type")
    public Result countRelationType(RelationType relationType){
        List<RelationType> relationTypeList = relationTypeService.countRelationType(relationType);
        return Result.success(relationTypeList);
    }

    @PostMapping("api/save/poet")
    public Result saveRelationPoet(@RequestBody RelationPoet relationPoet){
        relationPoet.insertOrUpdate();
        relationPoet.setPoet1(new Poet().selectById(relationPoet.getPoetId1()));
        relationPoet.setPoet2(new Poet().selectById(relationPoet.getPoetId2()));
        relationPoet.setRelationType(new RelationType().selectById(relationPoet.getRelationTypeId()));
        return Result.success(relationPoet);
    }
    @PostMapping("api/del/poet/{id}")
    public Result saveRelationPoet(@PathVariable Integer id){
        relationPoetService.removeById(id);
        return Result.success();
    }
    @GetMapping("page/poet")
    public Result pageRelationPoet(PageParam param, RelationPoet relationPoet){
        LambdaQueryWrapper<RelationPoet> wrapper = Wrappers.lambdaQuery(relationPoet);
        PageParam<RelationPoet> page = relationPoetService.page(param, wrapper);
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            page.getRecords().forEach(e -> {
                e.setPoet1(new Poet().selectById(e.getPoetId1()));
                e.setPoet2(new Poet().selectById(e.getPoetId2()));
                e.setRelationType(new RelationType().selectById(e.getRelationTypeId()));
            });
        }
        return Result.success(page);
    }

    @GetMapping("list/poet")
    public Result listRelationPoet(RelationPoet relationPoet){
        LambdaQueryWrapper<RelationPoet> wrapper = Wrappers.lambdaQuery(relationPoet);
        List<RelationPoet> list = relationPoetService.list(wrapper);
        if (CollectionUtils.isNotEmpty(list)){
            list.forEach(e -> {
                e.setPoet1(new Poet().selectById(e.getPoetId1()));
                e.setPoet2(new Poet().selectById(e.getPoetId2()));
                e.setRelationType(new RelationType().selectById(e.getRelationTypeId()));
            });
        }
        return Result.success(list);
    }


}
